﻿Imports System.Data.OleDb

Public Class addnewreview
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dr As OleDbDataReader

        If Not Page.IsPostBack Then
            Try
                Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
                oleDbConn.Open()
                Dim TagExistCmd As OleDbCommand = New OleDbCommand("SELECT m_title FROM movies Where m_id=@F1", oleDbConn)
                TagExistCmd.Parameters.AddWithValue("@f1", Request.QueryString("WuggiQuery"))
                dr = TagExistCmd.ExecuteReader()
                dr.Read()
                TB_m_title2.Text = "Re : " & dr.GetValue(0)
            Catch ex As Exception
                TB_m_title2.Text = ""
            End Try
        End If
    End Sub


    Protected Sub add_Click(sender As Object, e As EventArgs) Handles add.Click

        Dim sql As String = "INSERT INTO reviews (r_title, r_review, r_addby, r_addd, m_id) VALUES (@f1,@f2,@f3,NOW(),@f4)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", TB_m_title2.Text)
        cmd.Parameters.AddWithValue("@f2", TB_m_desc2.Text)
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f4", Request.QueryString("WuggiQuery"))
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Response.Redirect("Details.aspx?WuggiQuery=" & Request.QueryString("WuggiQuery"))

    End Sub

    Protected Sub btn_Rate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Rate.Click

        Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim sql As String = "DELETE FROM rating WHERE m_id=@f1 AND rat_user=@f2"
        Dim cmd As New OleDbCommand(sql, oleDbConn)
        cmd.Parameters.AddWithValue("@f1", Request.QueryString("WuggiQuery"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()


        Dim sql2 As String = "INSERT INTO rating (m_id,rat_user,rat_value) VALUES (@f1,@f2,@f3)"
        Dim cmd2 As New OleDbCommand(sql2, oleDbConn)
        cmd2.Parameters.AddWithValue("@f1", Request.QueryString("WuggiQuery"))
        cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd2.Parameters.AddWithValue("@f3", Rating1.CurrentRating)
        cmd2.ExecuteNonQuery()
        Message.Text = " Thank you for rating "
    End Sub

    Protected Sub Tag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Tag.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim Sql As String = "INSERT INTO movie_tags (m_id, t_id) VALUES (@f1,@f2)"
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                Dim cmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("WuggiQuery"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If tags.Text = "" Then Return

        Dim newTags = tags.Text.Split(",")

        For Each newTag In newTags

            Dim tagId As Double
            Dim dr As OleDbDataReader

            Try

                Dim TagCmd As OleDbCommand = New OleDbCommand("SELECT m_id FROM tags Where t_names=@F1", oleDbConn)
                TagCmd.Parameters.AddWithValue("@f1", newTag)
                dr = TagCmd.ExecuteReader()
                dr.Read()
                tagId = dr.GetValue(0)
            Catch ex As Exception
                tagId = 0
            End Try



            If tagId <= 0 Then

                Dim sql2 As String = "INSERT INTO tags(t_names) VALUES(@f1)"
                Dim cmd As OleDbCommand = New OleDbCommand(sql2, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", newTag)
                cmd.ExecuteNonQuery()
            End If

        Next
        Response.Redirect("tags.aspx")

    End Sub

End Class